package com.burgeon.backend.Rest.Tags;
/**
 * 获取会员标签
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.entity.VipTags;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class GetTagsALL extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetTagsALL(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public HashMap getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(), url);
        System.err.println(str);

        HashMap rows = new HashMap();
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)){
            throw new Exception("获取系统vip标签数据错误");
        }

        String count = new JSONArray(str).getJSONObject(0).getString("count");

        rows.put("count", count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<VipTags> list = new ArrayList<VipTags>();
        ArrayList<String> syn = new ArrayList<>();
        HashSet hashSet = new HashSet();

        for (int i=0;i<jsonArray.length();i++){
            VipTags vipTags = new VipTags();
            vipTags.setId(jsonArray.getJSONArray(i).getString(0));
            vipTags.setType(jsonArray.getJSONArray(i).getString(1));
            vipTags.setName(jsonArray.getJSONArray(i).getString(2));
            vipTags.setTypeid(jsonArray.getJSONArray(i).getString(3));
            list.add(vipTags);
            syn.add(vipTags.getType()+":"+vipTags.getName());
            HashMap hashMap = new HashMap();
            hashMap.put("id",vipTags.getTypeid());
            hashMap.put("name",vipTags.getType());
            hashSet.add(hashMap);
        }

        rows.put("set",hashSet);
        rows.put("data", list);
        rows.put("syn",syn);

        return rows;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");
        columns.add("'C_VIPATTRIBDEF_ID;NAME'");
        columns.add("NAME");
        columns.add("C_VIPATTRIBDEF_ID");

        params.put("columns", columns);
        params.put("table", "C_VIPATTRIBVALUE");//表名

        params.put("count", "true");

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetTagsALL g = new GetTagsALL("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
